package vanilla_technology.event;

import net.minecraft.block.Block;
import net.minecraft.util.EnumHand;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameData;
import vanilla_technology.building.Building;
import vanilla_technology.building.Buildings;

import java.lang.reflect.Method;

/**
 * Created by winston_wang on 2017/5/12.
 */
public class VTEvent {
    Buildings buildings_instance = new Buildings();
    public VTEvent(){
        MinecraftForge.EVENT_BUS.register(this);
    }
    @SubscribeEvent
    public void clickEvent(PlayerInteractEvent.RightClickBlock event){
        if(!event.getWorld().isRemote
                &&event.getHand()== EnumHand.MAIN_HAND) {
            Block triggerBlock = event.getWorld().getBlockState(event.getPos()).getBlock();
            for (Method method : Buildings.class.getDeclaredMethods()) {
                if (method.isAnnotationPresent(Building.class)) {
                    String name = GameData.getBlockRegistry().getNameForObject(triggerBlock).toString();
                    Building annotation = (Building) method.getAnnotation(Building.class);
                    if (name.equals(annotation.triggerBlock())) {
                        try {
                            method.invoke(buildings_instance, event.getEntityPlayer(), event.getPos());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
